var t = require;
var e = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var a = (function () {
        function t(t, e) {
            void 0 === t && (t = 0.5);
            void 0 === e && (e = {});
            this.prob = t;
            this.val = e;
        }
        t.prototype.setData = function (t) {
            this.prob = t.prob;
            this.val = t.val;
        };
        return t;
    })(),
    i = (function () {
        function t(t) {
            void 0 === t && (t = Math.random);
            this.rand_array = [];
            this.vec_probs = [];
            this.probs_other_id = [];
            this.b_init = !1;
            this.remberKey = [];
            this.random = t;
        }
        t.prototype.clear = function () {
            this.rand_array = [];
        };
        t.prototype.canRandomList = function () {
            return this.rand_array.length > 0;
        };
        t.prototype.setVals = function (t) {
            this.clear();
            if (t) for (var e in t) this.addVal(t[e]);
        };
        t.prototype.addVal = function (t, e) {
            var o;
            o = t instanceof Object ? new a(t.prob, t.val) : new a(t, e);
            this.rand_array.push(o);
        };
        t.prototype.addValCustom = function (t, e) {
            this.addVal(new a(t, e));
        };
        t.prototype._getNextRandomIndex = function () {
            this.b_init || this._settingProbs();
            if (this.vec_probs.length > 0) {
                var t = Math.floor(this.random() * this.vec_probs.length),
                    e = this.random();
                if (e > this.vec_probs[t] || e < 1e-6) {
                    var o = this.probs_other_id[t];
                    if (-1 != o) return o;
                }
                return t;
            }
            return 0;
        };
        t.prototype._settingProbs = function () {
            this.b_init = !0;
            this.vec_probs = [];
            this.probs_other_id = [];
            for (var t = this.rand_array.length, e = 0, o = 0; o < t; o++) {
                this.vec_probs.push(0);
                this.probs_other_id.push(-1);
                e += this.rand_array[o].prob;
            }
            for (o = 0; o < t; ++o) this.vec_probs[o] = 0 != e ? (this.rand_array[o].prob / e) * t : 0;
            for (o = 0; o < t; ++o)
                if (this.vec_probs[o] > 1)
                    for (var a = 0; a < t; ++a) {
                        if (a != o && this.vec_probs[a] < 1 && -1 == this.probs_other_id[a]) {
                            this.vec_probs[o] = this.vec_probs[o] + this.vec_probs[a] - 1;
                            this.probs_other_id[a] = o;
                        }
                        if (this.vec_probs[o] < 1) break;
                    }
        };
        t.prototype.getRandomVal = function (t) {
            void 0 === t && (t = null);
            return (t = null != t ? t : this.getRandomIndex()) < this.rand_array.length ? this.rand_array[t].val : null;
        };
        t.prototype.getValByIndex = function (t) {
            return this.getRandomVal(t);
        };
        t.prototype.getRandomIndex = function () {
            return this._getNextRandomIndex();
        };
        t.prototype.getValByCyclicIndex = function (t) {
            return this.getValByIndex(Math.floor(t) % this.rand_array.length);
        };
        return t;
    })();
o.default = i;
